local skel = fk.CreateSkill {
  name = "emo__jingpo",
  tags = {Skill.Switch},
}

Fk:loadTranslationTable{
  ["emo__jingpo"] = "惊魄",
  [":emo__jingpo"] = "转换技，每回合限一次，"..
  "阳：你需要使用即时牌时，可以摸一张牌，若与所需牌同类，你视为使用之；"..
  "阴：你或其他角色使用牌指定对方为目标后，你可以弃置对方一张牌，若与使用牌同类，此牌无效。",

  [":emo__jingpo_yang"] = "转换技，每回合限一次，"..
  "<font color='#E0DB2F'>阳：你需要使用即时牌时，可以摸一张牌，若与所需牌同类，你视为使用之；</font>"..
  "<font color='grey'>阴：你或其他角色使用牌指定对方为目标后，你可以弃置对方一张牌，若与使用牌同类，此牌无效。</font>",

  [":emo__jingpo_yin"] = "转换技，每回合限一次，"..
  "<font color='grey'>阳：你需要使用即时牌时，可以摸一张牌，若与所需牌同类，你视为使用之；</font>"..
  "<font color='#E0DB2F'>阴：你或其他角色使用牌指定对方为目标后，你可以弃置对方一张牌，若与使用牌同类，此牌无效。</font>",

  ["#emo__jingpo-yang"] = "惊魄:你可以摸一张牌，若为你选择的类型，视为使用之",
  ["#emo__jingpo-throw"] = "惊魄:你可以弃置 %src 一张牌，若为%arg，%arg2无效",
}

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic,normal_trick",
  prompt = "#emo__jingpo-yang",
  interaction = function(self, player)
    local all_choices = Fk:getAllCardNames("bt")
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = Util.FalseFunc,
  filter_pattern = {
    min_num = 0, max_num = 0, pattern = ".|0|nosuit|none", subcards = {},
  },
  view_as = function (self, player, cards)
    if Fk.all_card_types[self.interaction.data] == nil then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    return c
  end,
  before_use = function(self, player, use)
    local cards = player:drawCards(1, skel.name)
    if #cards == 0 or Fk:getCardById(cards[1], true).type ~= use.card.type then return self.name end
    use.extra_data = use.extra_data or {}
    -- 耦合一下，转换视为技切状态太早了，切换到阴状态的时候，use事件都没开始
    use.extra_data["emo__shuanghun"] = true
  end,
  enabled_at_play = function(self, player)
    if player:usedSkillTimes(skel.name) > 0 then return false end
    return player:getSwitchSkillState(skel.name) == fk.SwitchYang
  end,
  enabled_at_response = function (self, player, response)
    if player:usedSkillTimes(skel.name) > 0 then return false end
    if not response then
      return player:getSwitchSkillState(skel.name) == fk.SwitchYang
    end
  end,
})

skel:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and player:getSwitchSkillState(skel.name) == fk.SwitchYin and (target == player or data.to == player) then
      if player:usedSkillTimes(skel.name) > 0 then return false end
      local other = target == player and data.to or data.from
      return other and other ~= player and not other.dead and not other:isNude()
    end
  end,
  on_cost = function (self, event, target, player, data)
    local other = target == player and data.to or data.from
    if player.room:askToSkillInvoke(player, {
      skill_name = skel.name,
      prompt = "#emo__jingpo-throw:"..other.id .."::" .. data.card:getTypeString() .. ":" .. data.card:toLogString()
    }) then
      event:setCostData(self, { tos = {other} })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local other = target == player and data.to or data.from
    if other:isNude() then return false end
    local cid = room:askToChooseCard(player, {
      target = other, flag = "he", skill_name = skel.name
    })
    room:throwCard(cid, skel.name, other, player)
    if Fk:getCardById(cid, true).type == data.card.type then
      data.use.nullifiedTargets = table.simpleClone(room.players)
      if data.card.type == Card.TypeEquip or data.card.sub_type == Card.SubtypeDelayedTrick then
        data:cancelAllTarget()
      end
    end
  end,
})

return skel
